﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MDS = TrueCloud.SF.DocoTool.Salesforce.Metadata.WebService;
using SFS = TrueCloud.SF.DocoTool.Salesforce.Partner.WebService;
using System.Windows.Forms;

namespace TrueCloud.SF.DocoTool
{
    public class Connection
    {
        private string password;
        private string username;
        private string token;
        private string mdserverurl;
                
        //Accessors
	    public string Token
	{
		get { return token;}
		set { token = value;}
	}
	    public string Password
        {
            get{return password;}
            set{password = value;}
        }
        public string Username
        {
            get{return username;}
            set{username = value;}
        }
        public string MDServerURL 
        {
            get { return mdserverurl; }
            set { mdserverurl = value; }
        }
        
        public Connection(string strUserName, string strPassword, string strToken)
        {
            Password = strPassword;
            Username = strUserName;
            Token = strToken;
        }
        
        public bool ConnectTo(WebService service)
        {
            bool connection = false;

            switch (service)
            {
                case WebService.Sforce:
                    connection = ConnectToSForce();
                    break;
                case WebService.Metadata:
                    connection = ConnectToSForce();
                    if(connection)
                        connection = ConnectToMetadata();
                    break;
                default:
                    connection = false;
                    break;
            }

            return connection;
            
        }
        
        private bool ConnectToSForce()
        {
            Global.SFSoapClient = new SFS.SoapClient();
            SFS.LoginResult SFLogResult;

            try
            {
                SFLogResult = Global.SFSoapClient.login(new SFS.LoginScopeHeader(), new SFS.CallOptions(), Username, Password + Token);
            }
            catch (System.ServiceModel.FaultException fe)
            {
                MessageBox.Show(fe.Message, "SForce Service Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            catch (Exception)
            {
                throw;
            }
           

            //Once we have logged on we have to use the new URL and Session ID passed back by Salesforce
            Global.SFSoapClient = new SFS.SoapClient("Soap", SFLogResult.serverUrl);
            Global.SFSessionHeader.sessionId = SFLogResult.sessionId;
            Global.SFCallOptions.client = SFLogResult.userId;
            Global.SFCallOptions.defaultNamespace = "urn:partner.soap.sforce.com";
            MDServerURL = SFLogResult.metadataServerUrl;
            return true;
        }
       
        private bool ConnectToMetadata()
        {
            Global.MDSoapClient = new MDS.MetadataPortTypeClient("Metadata", MDServerURL);
            Global.MDSessionHeader.sessionId = Global.SFSessionHeader.sessionId;
            Global.MDCallOptions.client = Global.SFCallOptions.client;

            return true;
        }
    }
}
